package acom.test.swing.bb;

import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.RenderingHints;
import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;


public class MainPanel extends JPanel implements Observer{

	private static List<AnswerPeople> peoples = new ArrayList<AnswerPeople>();
	private static List<Prize> prizes = new ArrayList<Prize>();
	private Prize prize;
	
	private static final long serialVersionUID = 1L;
		

	private CardLayout cardLayout;
	
	private JPanel menuPanel;
	private JPanel switchPanel;	
	private JPanel buttonPanel;
	private JPanel tipPanel;
	private JLabel tip_lb;
		
	private DisplayPanel displayPanel;  //抽奖面板
	private ResultPanel resultPanel; //结果面板
	
	private int position_x=20;
	private int position_y=20;
	private static final int RUN=1;
	private static final int STOP=2;
	private static final int SHOW=3;
	
	
	public MainPanel() {
		this.setLayout(null);
		
		menuPanel = new MenuPanel();
		switchPanel = new JPanel();
		ButtonPanel butPn = new ButtonPanel();
		butPn.addObserver(this);
		buttonPanel  = butPn.getPane();
		
		cardLayout = new CardLayout();
		switchPanel.setLayout(cardLayout);
		
		displayPanel = new DisplayPanel();
		resultPanel = new ResultPanel();
		switchPanel.add(displayPanel,"disP");
		switchPanel.add(resultPanel,"resP");
		
		tipPanel = new JPanel();
		tip_lb = new JLabel("提示：点击开始前,先设置奖项、导入名单(excel格式文件)");
		tipPanel.add(tip_lb);
		
		init();
		
		this.add(menuPanel);
		this.add(switchPanel);
		this.add(buttonPanel);
		this.add(tipPanel);
	}

	private void init() {
		this.setBackground(Color.WHITE);
		menuPanel.setBounds(position_x+10,position_y-13,100,90);
		
		switchPanel.setBounds(position_x+130,position_y+155,702,425);
		
		buttonPanel.setBounds(position_x+130,position_y+600,700,55);
		
		tipPanel.setBounds(position_x+130,position_y+655,700,28);
		tipPanel.setLayout(null);
		tip_lb.setBounds(30,2,600,25);
		tip_lb.setForeground(Color.BLUE);
		
		buttonPanel.setOpaque(false);
		tipPanel.setOpaque(false);
	}
	
	

	@Override
	public void update(Observable o, Object arg) {
		int butNum=((ButtonPanel)o).getButNum();
		
		if(butNum==RUN){
			cardLayout.show(switchPanel, "disP");
			run_Action();
		}else if(butNum==STOP){
			cardLayout.show(switchPanel, "disP");
			stop_Action();
		}else if(butNum==SHOW){
			show_Action();
			cardLayout.show(switchPanel, "resP");			
		}
	}

	private void run_Action() {
		peoples=SystemUtil.getPeoplelist();
		prizes=SystemUtil.getPrizelist();
		if(peoples.size()<=0){
			JOptionPane.showMessageDialog(null, "候选人名单为空,请先导入名单");
			return ;
		}
		if(prizes.size()<=0){
			JOptionPane.showMessageDialog(null, "奖项为空,请先设置好奖项");
			return ;
		}
		
		//检查奖项 是否 抽完
		int finishPrizeNum = 0;	
		//本次 真正要抽的人数
		int viewNum = 0;
		for(Prize prize : prizes){
			if(prize.getIsFinish() || prize.getPersons().size()==prize.getTotal()){				
				finishPrizeNum++;
			}else{									
				SystemUtil.setCurPrize(prize);
				int temp =prize.getTotal()-prize.getPersons().size();
				if(temp>=prize.getFetchnum()){
					SystemUtil.setViewLabelNum(prize.getFetchnum());
					viewNum = prize.getFetchnum();
				}else{
					SystemUtil.setViewLabelNum(temp);
					viewNum = temp;
				}
				break;
			}
		}
		
		/**   一个很重要的问题  是   当一个奖项抽 到最后 一批时，实际要抽的人数是3 个，但是 显示的是10个，所有 需要控制 每次的抽奖人数   **/
		//System.out.println("finishPrizeNum==>"+finishPrizeNum);
		if(finishPrizeNum==prizes.size()){
			JOptionPane.showMessageDialog(null, "所有的奖项已经抽完");
			tip_lb.setText("提示：所有的奖项已经抽完");
			return ;
		}
			
		
				
		//各个奖项 一次抽的人数不一样, 决定着 DisplayPanel中JLabel 是否要重绘制
		prize = SystemUtil.getCurPrize();
		if(prize.getFetchnum()==SystemUtil.getTotalLabelNum()){
			//一样的话就 不管了
		}else {
			RepaintLabelsInDisplayPanel(prize.getFetchnum());
		}
		
		//提示当前抽的奖项 信息
		tip_lb.setText("提示：当前抽的奖项:"+prize.getPname()+";   本次要抽:"+viewNum+"人 ;   已抽人数/总奖数:"+prize.getPersons().size()+"/"+prize.getTotal());
		
		if(viewNum>peoples.size()){
			JOptionPane.showMessageDialog(null, "亲!! 候选人数不够了.  本次要抽奖的人数:"+viewNum+",候选人数："+peoples.size());
			return ;
		}
		
		if(!displayPanel.isRunning()){
			displayPanel.start();
		}		
	}
	
	private void RepaintLabelsInDisplayPanel(int fetchnum) {
		//System.out.println("DisplayPanel重绘");
		
		refreshView();
		
		//去掉原来的 Component
		displayPanel.removeAll();
		
		//DisplayPanel的布局
		Font font = null;
		if(fetchnum>80){
			displayPanel.setLayout(new GridLayout(12,2));
			font = new Font("宋体",Font.BOLD,14);
		}else if (fetchnum>50){
			displayPanel.setLayout(new GridLayout(10,2));
			font = new Font("宋体",Font.BOLD,20);
		}else if (fetchnum>20){
			displayPanel.setLayout(new GridLayout(8,2));
			font = new Font("宋体",Font.BOLD,28);
		}else{
			displayPanel.setLayout(new GridLayout(6,2));
			font = new Font("宋体",Font.BOLD,36);
		}
		
		
		//JLabel 的个数
		
		for(int i=0;i<fetchnum;i++){
			JLabel lab = new JLabel("",JLabel.CENTER);
			lab.setFont(font);
			displayPanel.add(lab);
		}
		
		//设置好了之后,别忘了
		SystemUtil.setTotalLabelNum(fetchnum);
	}
	
	//刷新 显示也防止有字体滞留
	private void refreshView(){
		cardLayout.show(switchPanel, "resP");	
		cardLayout.show(switchPanel, "disP");
	}
	
	private void stop_Action() {		
		if(displayPanel.isRunning()){
			displayPanel.stop();
		}		
	}
			
	private void show_Action(){
		resultPanel.clearTable();
		
		for(int i=prizes.size()-1;i>=0;i--){
			resultPanel.showPrizesResult(prizes.get(i));	
		}
		
	}
	
	@Override
	protected void paintComponent(Graphics g) {
		Graphics2D g2=(Graphics2D)g;
		ImageIcon back_icon = SystemUtil.createImageIcon("background.jpg");
		g2.drawImage(back_icon.getImage(),0,0,null);
	}
}
